package com.starleague.game;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.starleague.common.AbstractChildVisitable;
import com.starleague.common.IChildVisitor;

public abstract class AbstractGame<PLAYER extends AbstractPlayer<? extends AbstractGame<PLAYER, TEAM>, TEAM>, TEAM extends AbstractTeam<? extends AbstractGame<PLAYER, TEAM>, PLAYER>>
        extends AbstractChildVisitable
        implements IGame<PLAYER, TEAM>
{
    private String name;
    private final List<? extends TEAM> teamList = new ArrayList<>();

    protected AbstractGame(String name)
    {
        this.name = name;
    }

    protected AbstractGame()
    {
        // For serialization
    }

    @Override
    public String getName()
    {
        return this.name;
    }

    protected void setName(String name)
    {
        this.name = name;
    }

    @Override
    public List<? extends TEAM> getTeams()
    {
        return this.teamList;
    }

    @Override
    public Collection<? extends PLAYER> getPlayers()
    {
        List<PLAYER> playerList = new ArrayList<>();
        for (TEAM team : this.teamList)
            playerList.addAll(team.getPlayers());
        return playerList;
    }

    @Override
    public void accept(IChildVisitor visitor)
    {
        super.accept(visitor);
        visitor.visit("name", this.name);
        visitor.visit("teams", this.teamList);
    }
}
